﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Sisocana.Core;
using Sisocana.Core.Models;
using Sisocana.Core.Interfaces;
using Sisocana.Dal.EntityModels;
using Sisocana.Dal;
using Sisocana.Dal.Interfaces;
using System.Data;


namespace Sisocana.Core
{
    public class CSSCatService: ServiceBase<ICSSCatModel> ,ICSSCatService
    {
        
        public CSSCatService()
            : this(new CSSCatRepository(), new MasterPageCatRepository())
        {
        }

        private ICSSCatRepository _CSSCatRepository;
        private IMasterPageCatRepository _mpRepo = new MasterPageCatRepository();

        public CSSCatService(ICSSCatRepository RedesSocialesRepository, IMasterPageCatRepository mpRepo)
        {
            _CSSCatRepository = RedesSocialesRepository ?? new CSSCatRepository();
            _mpRepo = mpRepo ?? new MasterPageCatRepository();
        }

        public void createCSSCatMapperEF2Model()
        {
            AutoMapper.Mapper.CreateMap<CSSCat, CSSCatModel>()
                .ForMember(s => s.MasterPageName, opt => opt.MapFrom(c => c.MasterPageCat.nombreMaster));

        }

        public void createCSSCatMapperModel2EF()
        {
            AutoMapper.Mapper.CreateMap<CSSCatModel, CSSCat>();
        }


        public void createCSSCat(Models.CSSCatModel cssModel)
        {
            if (_CSSCatRepository.Count(c => c.nombreCSS.Trim().Equals(cssModel.nombreCSS, StringComparison.CurrentCultureIgnoreCase)) > 0)
                throw new Exception(String.Format("A stylesheet with name '{0}' already exists", cssModel.nombreCSS));

            createCSSCatMapperModel2EF();
            CSSCat css = new CSSCat();
            css = AutoMapper.Mapper.Map(cssModel, css);
            css.MasterPageCat = _mpRepo.GetSingle(mp => mp.nombreMaster == cssModel.MasterPageName);            
            css.MasterPageCat.CSSCat.Add(css);
            _CSSCatRepository.Save();
        }

        public IList<CSSCatModel> GetAll()
        {
            createCSSCatMapperEF2Model();

            IList<CSSCat> csssEF = _CSSCatRepository.GetAll();
            List<CSSCatModel> csss = new List<CSSCatModel>();
            foreach (CSSCat cssEF in csssEF)
            {
                CSSCatModel css = new CSSCatModel();
                css = AutoMapper.Mapper.Map(cssEF, css);
                csss.Add(css);
            }
            return csss;
        }


        public CSSCatModel GetSingle(System.Linq.Expressions.Expression<Func<CSSCatModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public void Add(CSSCatModel entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(CSSCatModel entity)
        {
            throw new NotImplementedException();
        }

        public void Update(CSSCatModel entity)
        {
            throw new NotImplementedException();
        }

        public IList<CSSCatModel> GetAll(System.Linq.Expressions.Expression<Func<CSSCatModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public IQueryable<CSSCatModel> Query(System.Linq.Expressions.Expression<Func<CSSCatModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count(System.Linq.Expressions.Expression<Func<CSSCatModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }
    }
}
